package com.jz.community.basecomm.base;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Build;
import android.view.View;

import com.jz.community.basecomm.utils.NetUtil;
import com.orhanobut.logger.Logger;
import com.tencent.smtt.sdk.WebView;
import com.tencent.smtt.sdk.WebViewClient;


public class BaseX5WebSetting {
    /**
     * 当前窗口执行JS脚本
     *
     * @param jsScript
     */
    public static void executeJS(final Activity mContext, final WebView webView, final String jsScript) {
        try {
            mContext.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    webView.loadUrl(jsScript);
                }
            });

        } catch (Exception e) {
        }
    }

    /**
     * 包含Client 的设置和基本设置
     *
     * @param mContext
     * @param mWebView
     */
    public void setAllWebview(final Activity mContext, final WebView mWebView) {
        setBaseWebview(mContext, mWebView);
    }

    /**
     * 基本设置
     *
     * @param mContext
     * @param mWebView
     */
    public void setBaseWebview(final Activity mContext, final WebView mWebView) {
        mWebView.getSettings().setJavaScriptEnabled(true);
        // 开启 database storage API 功能
        mWebView.getSettings().setDatabaseEnabled(true);
        mWebView.getSettings().setAllowFileAccess(true);
        // 设置加载进来的页面自适应手机屏幕
        mWebView.getSettings().setUseWideViewPort(true);
        // 开启 DOM storage API 功能
        mWebView.getSettings().setDomStorageEnabled(true);
        mWebView.getSettings().setBlockNetworkImage(false);
        mWebView.getSettings().setLoadWithOverviewMode(true);
        mWebView.getSettings().setSupportZoom(true);
        mWebView.getSettings().setBuiltInZoomControls(true);
        mWebView.getSettings().setDisplayZoomControls(true);

        //设置渲染的优先级
        mWebView.getSettings().setRenderPriority(com.tencent.smtt.sdk.WebSettings.RenderPriority.HIGH);
        // 支持多窗口
        mWebView.getSettings().setSupportMultipleWindows(true);

        setImagesLoading(mWebView);
        //设置  Application Caches 缓存目录
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            mWebView.getSettings().setAllowUniversalAccessFromFileURLs(true);
        }

        //开启 Application Caches 功能
        mWebView.getSettings().setAppCacheEnabled(true);
        // 设置 缓存模式
        if (NetUtil.isNetworkAvailable(mContext.getApplicationContext())) {
            mWebView.getSettings().setCacheMode(com.tencent.smtt.sdk.WebSettings.LOAD_DEFAULT);
        } else {
            mWebView.getSettings().setCacheMode(
                    com.tencent.smtt.sdk.WebSettings.LOAD_CACHE_ELSE_NETWORK);
        }

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            //两者都可以
            mWebView.getSettings().setMixedContentMode(mWebView.getSettings().getMixedContentMode());
            //mWebView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        }
    }

    //优化，对于版本小于19的，不立即加载图片
    public void setImagesLoading(WebView webLoadImg) {
        if (Build.VERSION.SDK_INT >= 19) {
            webLoadImg.getSettings().setLoadsImagesAutomatically(true);
        } else {
            webLoadImg.getSettings().setLoadsImagesAutomatically(false);
        }
    }


}
